package com.linkage.bss.crm.packagemgr.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.linkage.bss.commons.dao.Page;
import com.linkage.bss.commons.util.JsonUtil;
import com.linkage.bss.crm.packagemgr.dto.PackInfoDto;
import com.linkage.bss.crm.packagemgr.model.ActionOrderItem;
import com.linkage.bss.crm.packagemgr.model.ActionParameter;
import com.linkage.bss.crm.packagemgr.model.Package;
import com.linkage.bss.crm.packagemgr.model.PackageObject;
import com.linkage.bss.crm.packagemgr.model.PackageParameter;
import com.linkage.bss.crm.packagemgr.model.ParameterActionRela;
import com.linkage.bss.crm.packagemgr.smo.IPackageCheckSMO;
import com.linkage.bss.crm.packagemgr.smo.IPackageManagerSMO;
import com.linkage.bss.crm.packagemgr.util.Constant;
import com.linkage.bss.crm.packagemgr.util.DataGridManager;

/**
 *
 * 包管理action
 * @author  olns
 * @version  [版本号, 2013-4-28]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class PackageManagerAction extends DataGridManager {

	private static final long serialVersionUID = 9111105897562933813L;

	private IPackageManagerSMO packageManagerSMO = null;

	private IPackageCheckSMO packageCheckSMO;

	public IPackageManagerSMO getPackageManagerSMO() {

		return packageManagerSMO;
	}

	public void setPackageManagerSMO(IPackageManagerSMO packageManagerSMO) {

		this.packageManagerSMO = packageManagerSMO;
	}

	public IPackageCheckSMO getPackageCheckSMO() {
		return packageCheckSMO;
	}

	public void setPackageCheckSMO(IPackageCheckSMO packageCheckSMO) {
		this.packageCheckSMO = packageCheckSMO;
	}

	// 分页查询包
	public String getPackages() throws UnsupportedEncodingException {

		// 封装查询条件
		String packageName = ServletActionContext.getRequest().getParameter("packageName");
		String packageId = ServletActionContext.getRequest().getParameter("packageId");
		String packageCode = ServletActionContext.getRequest().getParameter("packageCode");
		String manageArea = ServletActionContext.getRequest().getParameter("manageArea");
		String startDt = ServletActionContext.getRequest().getParameter("startDt");
		String endDt = ServletActionContext.getRequest().getParameter("endDt");
		String packageStatus = ServletActionContext.getRequest().getParameter("packageStatus");
		String pubArea = ServletActionContext.getRequest().getParameter("pubArea");
		String pubChannel = ServletActionContext.getRequest().getParameter("pubChannel");

		Map<String, Object> queryMap = new HashMap<String, Object>();
		if (pubChannel != null && !pubChannel.equals("")) {
			queryMap.put("pubChannel", pubChannel);
		}

		if (pubArea != null && !pubArea.equals("")) {
			queryMap.put("pubArea", pubArea);
		}
		if (packageName != null && !packageName.equals("")) {
			packageName = URLDecoder.decode(packageName, "UTF-8");
			queryMap.put("packageName", packageName);
		}
		if (manageArea != null && !manageArea.equals("")) {
			queryMap.put("manageArea", manageArea);
		}
		if (packageCode != null && !packageCode.equals("")) {
			queryMap.put("packageCode", packageCode);
		}
		if (packageId != null && !packageId.equals("")) {
			queryMap.put("packageId", packageId);
		}
		if (startDt != null && !startDt.equals("")) {
			queryMap.put("startDt", startDt);
		}
		if (endDt != null && !endDt.equals("")) {
			queryMap.put("endDt", endDt);
		}
		if (packageStatus != null && !packageStatus.equals("")) {
			queryMap.put("packageStatus", packageStatus);
		}
		int total = packageManagerSMO.getPackageByCondCount(queryMap);

		Page queryPage = new Page();

		queryPage.setTotalSize(total);
		queryPage.setPageSize(rows);
		queryPage.init();
		queryPage.setCurPage(page);

		List<Package> packageList = packageManagerSMO.getPackageByCond(queryMap, queryPage);
		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		jsonMap.put("total", total);// total键 存放总记录数，必须的
		jsonMap.put("rows", packageList);// rows键 存放每页记录 list

		// json转化属性过滤，忽略掉值为null的属性
		result = JSONObject.fromObject(jsonMap, jsonConfig);// 格式化result
		// 一定要是JSONObject

		return SUCCESS;
	}

	private Package constructPackageObj(JSONObject packJson) {
		//构造赋值
		Package pack = (Package) JsonUtil.getObject(packJson.getString("packageBaseInfo"), Package.class);
		List<PackageObject> packageObjList = JsonUtil.getObjectList(packJson.getString("packObjMap"),
						PackageObject.class);
		List<PackageParameter> packageParameterList = JsonUtil.getObjectList(packJson.getString("packParamMap"),
						PackageParameter.class);
		List<ActionOrderItem> actionOrderItemList = JsonUtil.getObjectList(packJson.getString("actionItemMap"),
						ActionOrderItem.class);
		List<ParameterActionRela> paramActionRelaList = JsonUtil.getObjectList(
						packJson.getString("paramActionRelaMap"), ParameterActionRela.class);

		if (null != actionOrderItemList && actionOrderItemList.size() > 0) {
			for (ActionOrderItem actionOrderItem : actionOrderItemList) {
				List actionParamObjs = actionOrderItem.getActionItemParameterList();
				if (null != actionParamObjs && actionParamObjs.size() > 0) {
					List<ActionParameter> actionParams = new ArrayList<ActionParameter>();
					for (int i = 0; i < actionParamObjs.size(); i++) {
						Object obj = actionParamObjs.get(i);
						ActionParameter actionParam = (ActionParameter) JsonUtil.getObject(JsonUtil.getJsonString(obj),
										ActionParameter.class);
						actionParams.add(actionParam);
					}
					actionOrderItem.setActionItemParameterList(actionParams);
				}
			}
		}
		pack.setPackageObjList(packageObjList);
		pack.setPackageParameterList(packageParameterList);
		pack.setActionOrderItemList(actionOrderItemList);
		pack.setParamActionRelaList(paramActionRelaList);
		return pack;
	}

	/**
	 * 包新增
	 * @return String [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public String savePackage() {

		//取值
		String packJsonStr = ServletActionContext.getRequest().getParameter("pack");
		String operate = ServletActionContext.getRequest().getParameter("operate");
		JSONObject packJson = JSONObject.fromObject(packJsonStr);

		//构造赋值
		Package pack = constructPackageObj(packJson);

		pack.setStatusCd("1");
		//入库
		packageManagerSMO.savePackage(pack);

		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		jsonMap.put("result", "0");// total键 存放总记录数，必须的
		if (Constant.PACK_OPERATE_ADD.equals(operate)) {
			jsonMap.put("msg", "新增成功");
		}
		if (Constant.PACK_OPERATE_COPYNEW.equals(operate)) {
			jsonMap.put("msg", "复制新建成功");
		}

		result = JSONObject.fromObject(jsonMap, jsonConfig);
		return SUCCESS;
	}

	public String displayPackage() {

		String packageIdStr = ServletActionContext.getRequest().getParameter("packageId");
		String operate = ServletActionContext.getRequest().getParameter("operate");
		Long packageId = Long.valueOf(packageIdStr);
		PackInfoDto packInfoDto = packageManagerSMO.queryPackInfoByPackId(packageId);
		String resultString = JsonUtil.getJsonString(packInfoDto);
		ServletActionContext.getRequest().setAttribute("pack", resultString);
		ServletActionContext.getRequest().setAttribute("operate", operate);
		return SUCCESS;
	}

	public String getPackageDto() {
		String packageIdStr = ServletActionContext.getRequest().getParameter("packageId");
		Long packageId = Long.valueOf(packageIdStr);
		PackInfoDto packInfoDto = packageManagerSMO.queryPackInfoByPackId(packageId);
		result = JSONObject.fromObject(packInfoDto, jsonConfig);
		return SUCCESS;

	}

	/**
	 * 包修改,复制新建展示
	 *
	 * @return String [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public String modifyDisplayPackage() {

		String packageIdStr = ServletActionContext.getRequest().getParameter("packageId");
		String operate = ServletActionContext.getRequest().getParameter("operate");
		Long packageId = Long.valueOf(packageIdStr);
		Package pack = packageManagerSMO.getPackageByID(packageId);
		result = JSONObject.fromObject(pack, jsonConfig);
		ServletActionContext.getRequest().setAttribute("pack", result.toString());
		ServletActionContext.getRequest().setAttribute("operate", operate);
		return SUCCESS;
	}

	/**
	 * 包修改
	 *
	 * @return String [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public String modifyPackage() {
		//取值
		String packJsonStr = ServletActionContext.getRequest().getParameter("pack");
		String operate = ServletActionContext.getRequest().getParameter("operate");
		JSONObject packJson = JSONObject.fromObject(packJsonStr);

		//构造赋值
		Package pack = constructPackageObj(packJson);

		Object[] addItem = {};
		Object[] modifyItem = {};
		Object[] delItem = {};
		Map operateMap = new HashMap();
		JSONArray ads = packJson.getJSONArray("packContentAddArray");
		if (!packJson.getJSONArray("packContentAddArray").isEmpty()) {
			addItem = packJson.getJSONArray("packContentAddArray").toArray();
			operateMap.put("add", addItem);
		}
		if (!packJson.getJSONArray("packContentModifyArray").isEmpty()) {
			modifyItem = packJson.getJSONArray("packContentModifyArray").toArray();
			operateMap.put("modify", modifyItem);
		}
		if (!packJson.getJSONArray("packContentDelArray").isEmpty()) {
			delItem = packJson.getJSONArray("packContentDelArray").toArray();
			operateMap.put("del", delItem);
		}

		operateMap.put("operate", operate);
		//入库
		boolean flag = packageManagerSMO.updatePackage(pack, operateMap);

		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		if (flag) {
			jsonMap.put("result", "0");// total键 存放总记录数，必须的
			jsonMap.put("msg", "修改成功");// rows键 存放每页记录 list
		} else {
			jsonMap.put("result", "1");// total键 存放总记录数，必须的
			jsonMap.put("msg", "修改失败");// rows键 存放每页记录 list
		}

		result = JSONObject.fromObject(jsonMap, jsonConfig);

		return SUCCESS;
	}

	public String checkPackage() {

		//取值
		String packJsonStr = ServletActionContext.getRequest().getParameter("pack");
		String operate = ServletActionContext.getRequest().getParameter("operate");
		JSONObject packJson = JSONObject.fromObject(packJsonStr);
		//构造赋值
		Package pack = constructPackageObj(packJson);

		String checkResult = packageCheckSMO.checkPackageConfig(pack);
		result = JSONObject.fromObject(checkResult, jsonConfig);
		return SUCCESS;
	}

	public String checkPackageById() {

		//取值
		String packId = ServletActionContext.getRequest().getParameter("packId");
		//构造赋值

		String checkResult = packageCheckSMO.checkPackageConfig(Long.valueOf(packId));
		result = JSONObject.fromObject(checkResult, jsonConfig);
		return SUCCESS;
	}

	public String delPackage() {
		String delPackStr = ServletActionContext.getRequest().getParameter("delPackList");
		JSONArray arr = JSONArray.fromObject(delPackStr);

		List<Package> packIdList = arr.toList(arr, Package.class);
		boolean isSuccss = packageManagerSMO.delPackage(packIdList);
		Map<String, Object> jsonMap = new HashMap<String, Object>();// 定义map
		if (isSuccss) {
			jsonMap.put("res", "200");
			jsonMap.put("resMsg", "删除成功");
		} else {
			jsonMap.put("res", "1");
			jsonMap.put("resMsg", "删除失败");
		}

		result = JSONObject.fromObject(jsonMap, jsonConfig);
		return SUCCESS;

	}

}
